Function Get-CMCommands 
{
    [CmdletBinding()]
    PARAM ()
    PROCESS {
       Get-Command -Name *-CM* -CommandType Function  | Sort-Object Name | Format-Table Name, Module
    }
}


function Connect-CMProvider
{
<#
.Synopsis
    Connect to the ConfigMgr Provider
.Description
    This cmdlet is used to Connect to the ConfigMgr Provider
.Parameter SiteServerName 
    ConfigMgr Primary Site Server Name
.Parameter Credentials
    Credentials (from get-credential)
.Example
    Connect-CMProvider
    Description
    -----------
    This command connects to the ConfigMgr Provider on the local system.
.Example
    Connect-CMProvider MYTESTLABServer
    Description
    -----------
    This command connects to the ConfigMgr Provider on the server named MYTESTLABServer
.Example
    Connect-CMProvider MYTESTLABServer $myCMCreds
    Description
    -----------
    This command connects to the ConfigMgr Provider on the server named MYTESTLABServer using the credentials contained in $myCMCreds
.Example
    Connect-CMProvider MYTESTLABServer get-credential
    Description
    -----------
    This command connects to the ConfigMgr Provider on the server named MYTESTLABServer and prompts for credentials.
.Outputs
    The SMS_ProviderLocation Instance for the site
.Link
    http://msdn.microsoft.com/en-us/library/cc145228.aspx
.Link
    http://msdn.microsoft.com/en-us/library/cc144553.aspx
.Notes
 NAME:      Connect-CMProvider
 AUTHOR:    Greg Ramsey
 LASTEDIT:  07/03/2009 01:13:10
#Requires -Version 2.0
#>

[CmdletBinding(
    SupportsShouldProcess=$False,
    SupportsTransactions=$False, 
    ConfirmImpact="None",
    DefaultParameterSetName="")]
param(
[Parameter(Position=0)]
[string]$ComputerName="." ,

[Parameter(Position=1)]
$Credential
)

Begin
{
}#Begin

Process
{

    if ($Credential -eq $null)
    {
        get-wmiobject -class SMS_ProviderLocation `
			-Namespace ROOT\SMS -Computer $ComputerName | where {$_.ProviderForLocalSite -eq "True"}
    }
    else 
    {
	   get-wmiobject -class SMS_ProviderLocation `
			-Namespace ROOT\SMS -Computer $ComputerName -Credential $Credential | where {$_.ProviderForLocalSite -eq "True"}
    }
}#Process

End
{
}#End
}



function Get-CMObject
{
<#
.Synopsis
    Returns the instances of a ConfigMgr Class.
.Description
    Returns the instances of a ConfigMgr Class.
.Parameter ComputerName 
    ConfigMgr Site Server Name
.Parameter Class
    (Required) Enter Class Name (e.g., SMS_Collection)    
.Parameter Filter 
    Enter WMI Filter if desired
.Parameter Credential 
    Enter Alternate Credentials if desired
.Example
    Get-CMObject SMS_Collection
        Returns all instances of the SMS_Collection Class on the local system
.Example
    Get-CMObject SMS_Collection MYTESTLABServer
        Returns all instances of the SMS_Collection Class on MYTESTLABServer
.Example
    Get-CMObject -Class SMS_Collection -Computername MYTESTLABServer
        Returns all instances of the SMS_Collection Class on MYTESTLABServer
.Example
    Get-CMObject MYTESTLABServer -class SMS_Collection -filter "CollectionID='LAB00023'"
        Returns Collection with filter CollectionID=LAB00023 on MYTESTLABServer
.Example
    Get-CMObject -Computername MYTESTLABServer -class SMS_Collection -credential get-credential
        Prompts for credentials, and returns all instances in the SMS_Collection class
.Example
    Get-CMObject -ComputerName MYTESTLABServer $Cred
        uses credentials in $Cred
 .Example
    Get-CMObject -ComputerName MYTESTLABServer -Class SMS_Package -filter "PackageID='LAB00023'" -credential $Cred
        Returns instance of SMSPackageID = LAB00023, using alternate credentials.
.Outputs
    Returns instances of the desires SMS Class (e.g., SMS_Collection, SMS_Package
.Link
    
.Notes
 NAME:      Get-CMObject
 AUTHOR:    Greg Ramsey
 LASTEDIT:  08/7/2009 00:39:05
#Requires -Version 2.0
#>

[CmdletBinding(
    SupportsShouldProcess=$False,
    SupportsTransactions=$False, 
    ConfirmImpact="None",
    DefaultParameterSetName="")]
param(
[Parameter(Position=0)]
[String]$ComputerName="." ,

[Parameter(Position=1)]
[String]$Class=$(throw "Class Parameter Required."),

[Parameter(Position=2)]
[String]$Filter=$null ,

[Parameter(Position=3)]
$Credential=$null
)

Begin
{
}#Begin

Process
{
    
    if ($Credential -eq $null)
    {
        $oSite = Connect-CMProvider -ComputerName $ComputerName
        $NS = "Root\SMS\Site_" + $oSite.SiteCode
        if($Filter -eq $null)
        {
            get-wmiobject -class $Class -Namespace $NS -ComputerName $oSite.Machine
        }
        else
        {
            get-wmiobject -class $Class -Namespace $NS -ComputerName $oSite.Machine -filter $Filter
        }
    }
    else 
    {
        $oSite = Connect-CMProvider -Computer $ComputerName -Credential $Credential
        $NS = "Root\SMS\Site_" + $oSite.SiteCode
        if ($Filter -eq $null)
        {
            get-wmiobject -class $Class -Namespace $NS -Credential $Credential -ComputerName $oSite.Machine
        }
        else
        {
            get-wmiobject -class $Class -Namespace $NS -Credential $Credential -ComputerName $oSite.Machine -filter $Filter 
        }
        
    }
}#Process

End
{
}#End
}



function Show-CMPackage
{
<#
.Synopsis
    Returns a Package (Package, Boot Image Package, Software Updates Package, etc), or all instances of all package types.
.Description
    Returns a Package (Package, Boot Image Package, Software Updates Package, etc), or all instances of all package types.
.Parameter SiteServerName 
    ConfigMgr Site Server Name
.Parameter PackageID
    Enter PackageID    
.Parameter Credential 
    Enter Alternate Credentials if required
.Example
    Show-CMPackage
.Example
    Show-CMPackage MYTESTLABServer
        Returns all instances of all package types (SMS_Package, SMS_SoftwareUpdatePackage, etc) on MYTESTLABServer
.Example
    Show-CMPackage MYTESTLABServer LAB00012 
        Returns Package LAB20012 on MYTESTLABServer
.Example
    Show-CMPackage MYTESTLABServer LAB00012 get-credential
        Prompts for credentials
.Example
    Show-CMPackage MYTESTLABServer LAB00012 $Cred
        uses credentials in $Cred
.Outputs
    Returns either an instance, or multiple instances from a package Class
.Link
 
.Notes
 NAME:      Show-CMPackage
 AUTHOR:    Greg Ramsey
 LASTEDIT:  08/07/2009 10:54:05
#Requires -Version 2.0
#>

[CmdletBinding(
    SupportsShouldProcess=$False,
    SupportsTransactions=$False, 
    ConfirmImpact="None",
    DefaultParameterSetName="")]
param(
[Parameter(Position=0)]
[String]$ComputerName="." ,

[Parameter(Position=1)]
[String]$PackageID ,

[Parameter(Position=2)]
[String]$Filter ,

[Parameter(Position=3)]
$Credential
)

Begin
{
}#Begin

Process
{
	Get-CMBootImagePackage -ComputerName $ComputerName -PackageID $PackageID -Filter $filter -Credential $Credential
	Get-CMDriverPackage -ComputerName $ComputerName -PackageID $PackageID -Filter $filter -Credential $Credential
	Get-CMImagePackage -ComputerName $ComputerName -PackageID $PackageID -Filter $filter -Credential $Credential
	Get-CMOSInstallPackage -ComputerName $ComputerName -PackageID $PackageID -Filter $filter -Credential $Credential
	Get-CMPackage -ComputerName $ComputerName -PackageID $PackageID -Filter $filter -Credential $Credential
	Get-CMSoftwareUpdatesPackage -ComputerName $ComputerName -PackageID $PackageID -Filter $filter -Credential $Credential
	Get-CMTSPackage -ComputerName $ComputerName -PackageID $PackageID -Filter $filter -Credential $Credential
}#Process

End
{
}#End
}




function Get-CMInboxes
{
<#
.Synopsis
    Returns count of files in ConfigMgr Site Inboxes.
.Description
    Returns count of files in ConfigMgr Site Inboxes.
.Parameter SiteServerName 
    ConfigMgr Site Server Name
.Example
    Get-CMInboxes
.Example
    Get-CMInboxes MYTESTLABServer
        Returns count of ConfigMgr Inboxes on MYTESTLABServer
.Outputs
    Returns either an instance, or multiple instances from the SMS_Package Class
.Link
    
.Notes
 NAME:      Get-CMInboxes
 AUTHOR:    Greg Ramsey
 LASTEDIT:  03/28/2010 00:39:05
#Requires -Version 2.0
#>

[CmdletBinding(
    SupportsShouldProcess=$False,
    SupportsTransactions=$False, 
    ConfirmImpact="None",
    DefaultParameterSetName="")]
param(
[Parameter(Position=0)]
[String]$ComputerName="." ,

[Parameter(Position=1)]
$Credential
)

Begin
{
}#Begin

Process
{   
    if ($Credential)
    {
        write-host "`$Credential cannot be used with this function, must run as current user :( "
    }
    else
    {
        $prov = Connect-CMProvider -ComputerName $ComputerName
        
    	Write-host "Reading \\$($prov.Machine)\SMS_$($prov.SiteCode)\inboxes"
		Get-ChildItem \\$($prov.machine)\SMS_$($prov.SiteCode)\inboxes -Recurse | Group-Object Directory | Where { $_.Count -gt 0 } | Select Count, Name | sort-object -property @{expression="Count";Ascending=$False}
    }
}#Process

End
{
}#End
}

function Get-CMTopConsoleUser
{
<#
.Synopsis
    Returns Top System Console User information from SMS_G_SYSTEM_CONSOLE_USAGE.
.Description
    Returns Top System Console User information from SMS_G_SYSTEM_CONSOLE_USAGE.
.Parameter SiteServerName 
    ConfigMgr Site Server Name
.Example
    Get-CMTopConsoleUser
        Returns all instances from the SMS_G_SYSTEM_CONSOLE_USAGE class.
.Example
    Get-CMTopConsoleUser MYTESTLABServer
        Returns all instances from the SMS_G_SYSTEM_CONSOLE_USAGE class on MYTESTLABServer
.Example
    Get-CMTopConsoleUser MYTESTLABServer -cred get-credential
        Returns all instances from the SMS_G_SYSTEM_CONSOLE_USAGE class on MYTESTLABServer and prompts for credentials
.Example
    Get-CMTopConsoleUser MYTESTLABServer -filter "ResourceID=646718"
        Returns all instances from the SMS_G_SYSTEM_CONSOLE_USAGE class on MYTESTLABServer for specified resourceID.
.Example
    Get-CMTopConsoleUser MYTESTLABServer -filter "TopConsoleUser=Lab\Greg_ramsey"
        Returns all instances from the SMS_G_SYSTEM_CONSOLE_USAGE class on MYTESTLABServer where LAB\Greg_Ramsey is the top console user.
.Outputs
    Returns Top System Console User information from SMS_G_SYSTEM_CONSOLE_USAGE.
.Link
    
.Notes
 NAME:      Get-CMTopConsoleUser
 AUTHOR:    Greg Ramsey
 LASTEDIT:  03/28/2010 00:39:05
#Requires -Version 2.0
#>

[CmdletBinding(
    SupportsShouldProcess=$False,
    SupportsTransactions=$False, 
    ConfirmImpact="None",
    DefaultParameterSetName="")]
param(
[Parameter(Position=0)]
[String]$ComputerName="." ,

[Parameter(Position=2)]
[int32]$ResourceID=$Null ,

[Parameter(Position=3)]
[string]$TopConsoleUser=$Null ,

[Parameter(Position=4)]
$Credential
)

Begin
{
}#Begin

Process
{   
    if ($ResourceID -eq 0)
    {
        Get-CMObject -Computer $ComputerName -class "SMS_G_SYSTEM_SYSTEM_CONSOLE_USAGE" -Credential $Credential -Filter "TopConsoleUser LIKE '%$TopConsoleUser%'"
    }
    if ($ResourceID -ne 0)
    {
        Get-CMObject -Computer $ComputerName -class "SMS_G_SYSTEM_SYSTEM_CONSOLE_USAGE" -Credential $Credential -Filter "ResourceID = $ResourceID"
    }
    
}#Process

End
{
}#End
}


Function Get-SCCMInboxes {
	# Give a count of files in the SCCM-inboxes
	[CmdletBinding()]
	PARAM (
		[Parameter(Mandatory=$true, HelpMessage="SCCM Server",ValueFromPipeline=$true)][Alias("Server","SmsServer")][System.Object] $SccmServer,
		[Parameter(Mandatory=$false, HelpMessage="Minimum number of files in directory")][int32] $minCount = 1
	)

	PROCESS {
		Write-Verbose "Reading \\$($SccmServer.Machine)\SMS_$($SccmServer.SccmProvider.SiteCode)\inboxes"
		return Get-ChildItem \\$($SccmServer.Machine)\SMS_$($SccmServer.SccmProvider.SiteCode)\inboxes -Recurse | Group-Object Directory | Where { $_.Count -gt $minCount } | Format-Table Count, Name -AutoSize
	}
}

    function ping ($Computer)
	{

	#Check for network connectivity
    	(Gwmi Win32_PingStatus -filter "Address = '$Computer' and Timeout=10000").StatusCode
}

